package com.xjc.business.controller.stu;

import com.xjc.business.entity.po.stu.ExchangeDormitoryApplyPo;
import com.xjc.business.entity.po.stu.StayBedApplyPo;
import com.xjc.business.entity.vo.stu.*;
import com.xjc.business.service.BuildingsService;
import com.xjc.business.service.StudentChangeBedApplysService;
import com.xjc.business.service.StudentStayBedApplysService;
import com.xjc.business.service.TimePeriodsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 徐锦程
 * 时间:2025-02-06 12:33
 */
@RestController
@RequestMapping("/student/dormitory")
public class StuDormitoryController {

    @Autowired
    TimePeriodsService timePeriodsService;

    @Autowired
    BuildingsService buildingsService;

    @Autowired
    StudentChangeBedApplysService studentChangeBedApplysService;

    @Autowired
    StudentStayBedApplysService studentStayBedApplysService;

    /**
     * 获取当前选宿状态
     * @param id 学生微信id
     * @return
     */
    @RequestMapping("/get_select_dormitory_status/{id}")
    public Result<Boolean> getSelectDormitoryStatus(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return timePeriodsService.getSelectDormitoryStatus(id);
    }

    /**
     * 获取当前换宿状态
     * @param id 学生微信id
     * @return
     */
    @RequestMapping("/get_exchange_dormitory_status/{id}")
    public Result<Boolean> getExchangeDormitoryStatus(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return timePeriodsService.getExchangeDormitoryStatus(id);
    }

    /**
     * 获取当前留宿状态
     * @param id 学生微信id
     * @return
     */
    @RequestMapping("/get_stay_apply_status/{id}")
    public Result<Boolean> getStayBedApplyStatus(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return timePeriodsService.getStayBedApplyStatus(id);
    }

    /**
     * 获取宿舍信息
     * @param id 学生微信id
     * @return
     */
    @GetMapping("/get_dormitory_info/{id}")
    public Result<DormitoryVo> getDormitoryInfo(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        return buildingsService.getDormitoryInfo(id);
    }

    /**
     * 获取宿舍列表
     * @param id 学生微信id
     * @return 列表数据
     */
    @GetMapping("/get_building_list/{id}")
    public Result<List<BuildingVo>> getBuildingList(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingsService.getBuildingListWithStuId(id);
    }

    /**
     * 确认选宿
     * @param bedId 床位id
     * @param id 学生微信id
     * @return 反馈
     */
    @PostMapping("/sure_select_dormitory/{bedId}/{id}")
    public Result<String> sureSelectDormitory(@PathVariable(value = "bedId",required = false) Integer bedId,@PathVariable(value = "id",required = false) Integer id){
        if(id == null || bedId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingsService.sureSelectDormitory(id,bedId);
    }

    /**
     * 获取当前需要审批的他人换宿申请信息
     * @param id 学生微信id
     * @return 宿舍信息
     */
    @GetMapping("/get_current_sure_dormitory/{id}")
    public Result<CurrentSureDormitoryVo> getCurrentSureDormitory(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return studentChangeBedApplysService.getCurrentSureDormitory(id);
    }

    /**
     * 获取当前换宿申请信息
     * @param id 学生微信id
     * @return 宿舍信息
     */
    @GetMapping("/get_exchange_dormitory_apply/{id}")
    public Result<ExChangeDormitoryApplyVo> getExChangeDormitoryApply(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return studentChangeBedApplysService.getExChangeDormitoryApply(id);
    }

    /**
     * 提交换宿申请
     * @param exchangeDormitoryApplyPo 换宿申请信息
     * @return 反馈
     */
    @PostMapping("/sure_exchange_dormitory")
    public Result<String> sureExchangeDormitory(@RequestBody(required = false)ExchangeDormitoryApplyPo exchangeDormitoryApplyPo){
        if(exchangeDormitoryApplyPo == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        return studentChangeBedApplysService.sureExchangeDormitory(exchangeDormitoryApplyPo);
    }


    /**
     * 目标宿舍拥有者改变申请换宿状态
     * @param id 申请Id
     * @return 反馈
     */
    @PostMapping("/change_exchange_dormitoryApply_status/{id}/")
    public Result<String> changeExchangeDormitoryApplyStatus(
            @PathVariable(value = "id",required = false) Integer id
            ){
     if(id == null){
         throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
     }
     return studentChangeBedApplysService.changeExchangeDormitoryApplyStatus(id);
    }

    /**
     * 获取当前学生的留宿申请
     * @param id 学生微信id
     * @return Result
     */
    @GetMapping("/get_stay_bed_apply/{id}")
    public Result<StayBedApplyVo> getCurrentStayBedApply(@PathVariable(value = "id", required = false) Integer id){
        if (id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return studentStayBedApplysService.getCurrentStayBedApply(id);
    }

    /**
     * 获取当前学生的历史留宿申请
     * @param id 学生微信id
     * @return Result
     */
    @GetMapping("/get_history_stay_bed_apply/{id}")
    public Result<List<HistoryStayBedApplyVo>> getHistoryStayBedApplyList(@PathVariable(value = "id", required = false) Integer id){
        if (id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return studentStayBedApplysService.getHistoryStayBedApplyList(id);
    }

    /**
     * 提交留宿申请
     * @param stayBedApplyPo 留宿申请信息
     * @return 反馈
     */
    @PostMapping("/add_stay_bed_apply")
    public Result<String> addStayBedApply(@RequestBody(required = false) StayBedApplyPo stayBedApplyPo){
        return studentStayBedApplysService.addStayBedApply(stayBedApplyPo);
    }
}
